package com.n5x.model.user;

import com.n5x.model.AbstractModel;
import com.n5x.model.shopping.ShoppingCart;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.Alias;
import org.springframework.data.annotation.Transient;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Alias("userInfo")
public class UserInfo extends AbstractModel {

    public final static String RECOMMEND_PHONE = "phone";

    public final static String RECOMMEND_IS_DISCOUNT = "isDiscount";

    /**
     * 用户名称
     */
    private String name = "";

    /**
     * 余额
     */
    private BigDecimal balance = new BigDecimal(0);

    /**
     * 总充值金额
     */
    private BigDecimal recharge = new BigDecimal(0);

    /**
     * 总消费金额
     */
    private BigDecimal consume = new BigDecimal(0);

    /**
     * 会员等级关联字段
     */
    private String fkLevel;

    /**
     * 联系电话
     */
    private String phone = "";

    private String address;

    private String recommend;

    /**
     * 最后登陆时间
     */
    private Date lastLoginTime = new Date();

    /**
     * 最后登陆IP
     */
    private String lastLoginIp;

    /**
     * 用户购物车
     */
    @Transient
    private List<ShoppingCart> shoppingCarts;

    @Transient
    public JSONObject getRecommendJSON() {
        if (StringUtils.isNotBlank(this.recommend)) {
            return JSONObject.fromObject(this.recommend);
        }
        return null;
    }

    public UserInfo createRecommend(String phone) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(RECOMMEND_PHONE, phone);
        jsonObject.put(RECOMMEND_IS_DISCOUNT, "0");
        this.setRecommend(jsonObject.toString());
        return this;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public BigDecimal getRecharge() {
        return recharge;
    }

    public void setRecharge(BigDecimal recharge) {
        this.recharge = recharge;
    }

    public BigDecimal getConsume() {
        return consume;
    }

    public void setConsume(BigDecimal consume) {
        this.consume = consume;
    }

    public String getFkLevel() {
        if (StringUtils.isBlank(this.fkLevel))
            return "0";
        return fkLevel;
    }

    public void setFkLevel(String fkLevel) {
        this.fkLevel = fkLevel;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        if (address == null)
            return "";
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getRecommend() {
        return recommend;
    }

    public void setRecommend(String recommend) {
        this.recommend = recommend;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public UserInfo setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
        return this;
    }

    public String getLastLoginIp() {
        return lastLoginIp;
    }

    public UserInfo setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
        return this;
    }

    public List<ShoppingCart> getShoppingCarts() {
        return shoppingCarts;
    }

    public void setShoppingCarts(List<ShoppingCart> shoppingCarts) {
        this.shoppingCarts = shoppingCarts;
    }
}